import requests
import pickle

from RabbitDemo.RabbitMQClass import get_connection

MAX_PRIORITY = 100
QUEUE_NAME = 'scrape_queue'
connection = get_connection()
channel = connection.channel()
session = requests.Session()


def scrape(request):
    try:
        response = session.send(request.prepare())
        print('response.raw')
        print(response.raw)
        print(f'response {response.status_code}')
        print(f'success scraped url {response.url}')
    except requests.RequestException:
        print(f'error occurred when scraping {request.url}')


while True:
    method_frame, header, body = channel.basic_get(
        queue=QUEUE_NAME, auto_ack=True)
    if body:
        request = pickle.loads(body)
        print(f'Get {request}')
        scrape(request)
